package com.example.zhunongzonghe.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zhunongzonghe.beans.ZnFarmEvaluateBean;
import com.example.zhunongzonghe.common.Result;
import com.example.zhunongzonghe.service.ZnFarmEvaluateService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Principal;

@RestController
@RequestMapping("/znFarmEvaluate")
public class ZnFarmEvaluateController {

    @Resource
    private ZnFarmEvaluateService znFarmEvaluateService;
    /**
     * 用户新增评价
     * @param znFarmEvaluateBean
     * @param principal
     * @return
     */
    @PostMapping("/addEvaluate")
    public Result FarmEvaluate(@RequestBody ZnFarmEvaluateBean znFarmEvaluateBean, Principal principal){
        if (znFarmEvaluateBean.getScore()==null&&znFarmEvaluateBean.getContent()==null){
            return new Result(false,"数据异常",400);
        }
        znFarmEvaluateService.addFarmEvaluate(znFarmEvaluateBean,principal.getName());
        return new Result(true,"成功",200);
    }

    /**
     * 用户编辑当前商品评价
     * @param znFarmEvaluateBean
     * @param principal
     * @return
     */
    @PostMapping("/EditEvaluate")
    public Result EditEvaluate(@RequestBody ZnFarmEvaluateBean znFarmEvaluateBean, Principal principal){
        if (znFarmEvaluateBean.getScore()==null&&znFarmEvaluateBean.getContent()==null){
            return new Result(false,"数据异常",400);
        }
        znFarmEvaluateService.editFarmEvaluate(znFarmEvaluateBean,principal.getName());
        return new Result(true,"成功",200);
    }

    /**
     * 查询全部农产品评价
     * @param farmId
     * @param createPage
     * @param pageSize
     * @return
     */
    @GetMapping("/getFarmEvaluateAll")
    public Result selectEvaAll(@RequestParam("farmId")Integer farmId,
                               @RequestParam("createPage")Integer createPage,
                               @RequestParam("pageSize")Integer pageSize){
        if (farmId==null) return new Result(false,"数据异常",400);
        Page<ZnFarmEvaluateBean> beanPage = znFarmEvaluateService.selectEvaAll(farmId,createPage,pageSize);
        return new Result(true,"成功",200,beanPage);
    }

    /**
     * 查询评分为4和5的农产品评价
     * @param farmId
     * @param createPage
     * @param pageSize
     * @return
     */
    @GetMapping("/getFarmEvaluateFourAndFive")
    public Result selectFourAndFive(@RequestParam("farmId")Integer farmId,
                               @RequestParam("createPage")Integer createPage,
                               @RequestParam("pageSize")Integer pageSize){
        if (farmId==null) return new Result(false,"数据异常",400);
        System.out.println("======"+farmId);
        Page<ZnFarmEvaluateBean> beanPage = znFarmEvaluateService.selectFourAndFive(farmId,createPage,pageSize);
        return new Result(true,"成功",200,beanPage);
    }

    /**
     * 查询评分为3的农产品评价
     * @param farmId
     * @param createPage
     * @param pageSize
     * @return
     */
    @GetMapping("/getFarmEvaluateThree")
    public Result selectEvaThree(@RequestParam("farmId")Integer farmId,
                                @RequestParam("createPage")Integer createPage,
                                @RequestParam("pageSize")Integer pageSize){
        if (farmId==null) return new Result(false,"数据异常",400);
        Page<ZnFarmEvaluateBean> beanPage = znFarmEvaluateService.selectThree(farmId,createPage,pageSize);
        return new Result(true,"成功",200,beanPage);
    }

    /**
     * 查询评分为1和2的农产品评价
     * @param farmId
     * @param createPage
     * @param pageSize
     * @return
     */
    @GetMapping("/getFarmEvaluateOneAndTow")
    public Result selectOneAndTow(@RequestParam("farmId")Integer farmId,
                                @RequestParam("createPage")Integer createPage,
                                @RequestParam("pageSize")Integer pageSize){
        if (farmId==null) return new Result(false,"数据异常",400);
        Page<ZnFarmEvaluateBean> beanPage = znFarmEvaluateService.selectOneAndTow(farmId,createPage,pageSize);
        return new Result(true,"成功",200,beanPage);
    }

    /**
     * 后台查询全部农产品评价
     * @param currentPage
     * @param pageSize
     * @param searchInfo
     * @return
     */
    @GetMapping("/getMagEvaluateAll")
    public Result getMagEvaluateAll (@RequestParam("currentPage")Integer currentPage,
                                     @RequestParam("pageSize")Integer pageSize,
                                     @RequestParam("searchInfo")Object searchInfo){
        Page<ZnFarmEvaluateBean> beanPage = znFarmEvaluateService.managementAll(currentPage,pageSize,searchInfo);
        if (currentPage > beanPage.getPages()){
            beanPage=znFarmEvaluateService.managementAll((int) beanPage.getPages(),pageSize,searchInfo);
        }
        return new Result(true,"成功",200,beanPage);
    }

    /**
     * 后台删除评价
     * @param id
     * @return
     */
    @DeleteMapping("/DeleteFarmEvaluate/{id}")
    public Result deleteFarmEvaluate(@PathVariable Integer id){
        if (id==null) return new Result(false,"失败",400);
        znFarmEvaluateService.removeById(id);
        return new Result(true,"成功",200);
    }
}
